<!--tvm票款差异协查-->
<template>
  <div class="reportMistakeInfoAction el-card is-always-shadow el-card__body">

    <avue-crud
      ref="TvmJiezhangdanjuxiecha"
      :option="option"
      :table-loading="listLoading"
      :data="data"
      :page.sync="page"
      @search-change="handleFilter"
      @selection-change="selectionChange"
      @refresh-change="refreshChange"
      @on-load="getList">
      <template slot="menuRight">
        <el-button type="primary"  size="mini" @click="replaceContent()">一键替换</el-button>
        <el-button type="primary"  size="mini" @click="sendStatus">下发</el-button>
        <el-button type="primary"  size="mini" @click="exportData">导出</el-button>
      </template>
      <template slot-scope="scope" slot="lineCodeSearch">
        <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine">
          <el-option
            v-for="(item,index) in lineCodes"
            :key="index"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="stationCodeSearch">
        <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" @change="changeStation">
          <el-option
            v-for="(item,index) in stationCodes"
            :key="index"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="tvmCodeSearch">
        <el-select v-model="searchForm.tvmCode" placeholder="请选择" style="width: 150px" >
          <el-option
            v-for="(item,index) in tvmCodes"
            :key="index"
            :label="item.tvmCode"
            :value="item.tvmCode">
          </el-option>
        </el-select>
      </template>
      <template slot="search" slot-scope="{row,size}">
        <el-form-item label="原因类型">
        <el-select v-model="searchForm.electromeReason" :size="size" placeholder="请选择" style="width: 150px" >
          <el-option
            v-for="(item,index) in electromeReasons"
            :key="index"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        </el-form-item>
      </template>
      <template slot="searchMenu" slot-scope="{row,size}">
        <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm);getList()}" icon="el-icon-delete">清空</el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button size="small" type="text" v-if="scope.row.replyStatus==1" plain @click.stop="previewEditor(scope.row)">查看回馈</el-button>
      </template>
    </avue-crud>
    <HuikuiInfo ref="HuikuiInfo" v-if="isShowDia"></HuikuiInfo>
</div>
</template>


<script>
    import {getForm, postForm} from '@/api/gen/gen'
    import ExportJsonExcel from "js-export-excel";
   import HuikuiInfo from './HuikuiInfo';
    import {
      getSwAuTvmDailyBillAssistList, sendStatusSwAuTvmDailyBillAssist,
      setSwAuTvmDailyBillAssistDetail
    } from "../../../../../api/subwayRat/TVMRevenueVarianceAudit.TvmChaYiDXC/TvmJiezhangdanjuxiecha";
    import {
      electromeReasons, fenExYuangettoFixed, getElectromeReasons,
      getgetStationCodes,
      getLineCodes, getLineName, getRepalceStatus,
      getreplayStatusFormat, getStationName, getXieChaStatusFormat, repalceStatus,
      replayStatus, setSearchFiledsEmpty, xieChaStatus, getStationByCode,getLineAndStationByCode, getSwPmTvmCodeList
    } from "../../../../../api/subwayRat/commonCheck";
    import {exportDataToExcel} from "../../../../../api/subwayRat/exportSelf";
    import transitUtil from "../../../../../api/subwayRat/transitUtil";
    export default {
      components:{
        HuikuiInfo
      },
      data() {
            return {
              isShowDia:false,
              listLoading: true,
              lineCodes:[],
              stationCodes:[],
              tvmCodes:[],
              electromeReasons:electromeReasons(),
              searchForm:{
                lineCode:'',
                stationCode:''
              },
              data: [],
              selectKeys:[], //存放对像
              selectKeyVals:[], //存放值
              page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20, // 每页显示多少条,
                isAsc: false,// 是否倒序
              },
              option:{
                emptyBtn:false,
                editBtn:false,
                delBtn:false,
                addBtn:false,
                index:true,
                indexLabel:'序号',
                page:true,
                menuWidth:150,
                align:'center',
                menuAlign:'center',
                selection: true,
                reserveSelection:false,
                column:[
                  {prop:'lineCode',label:'线路名称',type:'select',search:true,searchslot:true,formatter: function(row, value , label, column){
                      return getLineName(value);
                    }},
                  {prop:'stationCode',label:'站点名称',type:'select',search:true,searchslot:true,formatter: function(row, value , label, column){
                      return getStationName(row.lineCode,value);
                    }},
                  {prop:'incomeTime',label:'收入日期'},
                  {prop:'tvmCode',label:'TVM编号',type:'select',search:true,searchslot:true,},
                  {prop:'cashTotal',label:'现金支付总金额',formatter:function (row,value , label, column) {
                      return fenExYuangettoFixed(row,'cashTotal');
                    }},
                  {prop:'elecTotal',label:'电子支付总金额',formatter:function (row,value , label, column) {
                      return fenExYuangettoFixed(row,'elecTotal');
                    }},
                  {prop:'incomeSettleBeginTime',label:'结账开始时间'},
                  {prop:'incomeSettleEndTime',label:'结账结束时间'},
                  {prop:'electromeReason',label:'归属机电专业',dicData:electromeReasons(),
                    formatter:function(row, value , label, column){
                      return getElectromeReasons(value);
                    }},
                  {prop:'assistInvestStatus',label:'协查状态',dicData:xieChaStatus(),
                    formatter:function(row, value , label, column){
                      return getXieChaStatusFormat(value);
                    }},
                  {prop:'replyStatus',label:'反馈状态',search:true,type:'select',
                    dicData:replayStatus(),
                    formatter:function(row, value , label, column){
                      return getreplayStatusFormat(value);
                    }},
                  {prop:'replaceStatus',label:'替换状态',dicData:repalceStatus(),
                    formatter:function(row, value , label, column){
                      return getRepalceStatus(value);
                    }}
                ]
              },
            }
        },
        created() {
        },
        methods: {
          setSearchFiledsEmpty,
          refreshChange() {
            this.getList()
          },
          getList() {
            this.listLoading = true;
            Object.assign(this.searchForm,this.page);
            getSwAuTvmDailyBillAssistList(this.searchForm).then((response)=>{
              this.data = response.data.data;
              this.listLoading = false;
              this.page.total=response.data.total;
            });
          },
          selectionChange(list){
            this.selectKeys=list;
            this.selectKeyVals=[];
            for(let i=0;i<list.length;i++){
              this.selectKeyVals.push(list[i].id);
            }
          },
          handleFilter(params, done) {
            Object.assign(this.searchForm,params);
            this.getList()
            done()
          },
          replaceContent(){ //一键替换
            for(let i=0;i<this.selectKeys.length;i++){
              if(this.selectKeys[i].replyStatus==0){
                this.$message.info("可替换已经反馈的数据！");
                return;
              }
            }
            let ids=[];
            for(let i=0;i<this.selectKeys.length;i++){
              if(this.selectKeys[i].replaceStatus==0){
                ids.push(this.selectKeys[i].id);
              }
            }
            if(ids.length==0){
              this.$message.warning("无有效替换数据！");
              return;
            }
            setSwAuTvmDailyBillAssistDetail({ids:ids.join(",")}).then(res=>{
              if(res.data.code==0){
                this.$message.success("操作成功！");
                this.getList();
                this.cancelChoose();
              }else{
                this.$message.error(res.data.msg);
              }
            }).catch(e=>{
              this.$message.error("操作失败！");
            });

          },
          previewEditor(row){ //回馈预览
            this.isShowDia=true;
            let th=this;
            this.$nextTick(()=>{
              th.$refs.HuikuiInfo.recallShow(row.id,row,3);
            })
          },
          async sendStatus(){
            if(this.selectKeys.length<=0){
              this.$message.warning("请选择下发数据！");
              return;
            }

            for(let i=0;i<this.selectKeys.length;i++){
              if(this.selectKeys[i].assistInvestStatus!=1){
                this.$message.warning("请选择未下发数据！");
                return;
              }
            }

            //下发
            let messageLSList = [];
            for(let i=0;i<this.selectKeys.length;i++){
              let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
              messageLSList.push({
                "content":"车站：" + data.data[0].name,
                "lineCode":this.selectKeys[i].lineCode,
                "stationCode":this.selectKeys[i].stationCode
              })
            }
            let p = {
              messageLSList:JSON.stringify(messageLSList),
              title:this.$store.getters.tag.label,
              msgType:"下发",
              type:"TvmJiezhangdanjuxiecha_001",
              ids:this.selectKeyVals.join(',')
            };
            sendStatusSwAuTvmDailyBillAssist(p).then(res=>{
              if(res.data.code==0){
                this.$message.success("操作成功！");
                this.getList();
              }else{
                this.$message.error("操作失败！");
              }
            })

          },
          changeLine(v){ //改变线路时
            getgetStationCodes(v).then(res=>{
              this.stationCodes=res;
              this.searchForm.stationCode='';
            })
          },
          changeStation(v){ //改变车站
            let param={};
            param.lineCode=this.searchForm.lineCode;
            param.stationCode=v;
            getSwPmTvmCodeList(param).then(r=>{
              this.tvmCodes=r.data;
            });
          },
          async exportData(){
            this.searchForm.pageNo=1;
            this.searchForm.pageSize=1000000000;
            if(this.selectKeys.length>0){
                exportDataToExcel(this.selectKeys,this.option.column,'TVM结账单据协查');
            }else {
              let res=await getSwAuTvmDailyBillAssistList(this.searchForm);
              let data = res.data.data;
              exportDataToExcel(data,this.option.column,'TVM结账单据协查');
            }
          },
          cancelChoose(){
            this.$refs.TvmJiezhangdanjuxiecha.toggleSelection();
          },
          otherVueAction(){ //供其他Vue页面调用
            let th=this;
            transitUtil.$on('jiezhangdan_call',(param)=>{
              console.log(param);
              getSwAuTvmDailyBillAssistList({relationid:param.id}).then((response)=>{
                th.data = response.data.data;
                th.previewEditor(th.data[0]);
              });
            });
            transitUtil.$on('jiezhangdan_view',(param)=>{
              this.searchForm.relationid = param.id;
              this.getList();
            });
          }
        },
      mounted() {
        getLineCodes().then(res=>{ //获取线路
          this.lineCodes=res;
        });
        this.otherVueAction();
      }
    }
</script>
<style lang="scss">

</style>
